package ysq.xyg.base.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import ysq.xyg.base.exception.UCException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Set;

/**
 * @author taocongcong
 * @create 2019-12-25 13:23
 */
@Component
@Aspect
public class RequestParamValidAspect {

    @Pointcut("execution(* ysq.xyg.*.controller.*.*(..))")
    public void controllerBefore() {
    }

    @Before("controllerBefore()")
    public void before(JoinPoint point) {
        Object target = point.getThis();
        // 获得切入方法参数
        Object[] args = point.getArgs();
        // 获得切入的方法
        Method method = ((MethodSignature) point.getSignature()).getMethod();
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        ExecutableValidator executableValidator = factory.getValidator().forExecutables();
        // 执行校验，获得校验结果
        Set<ConstraintViolation<Object>> validResult = executableValidator.validateParameters(target, method, args);
        // 如果有校验不通过的
        if (!validResult.isEmpty()) {
            // 直接抛出异常
            Iterator<ConstraintViolation<Object>> iterator = validResult.iterator();
            throw new UCException(iterator.next().getMessage());
        }
    }

}
